import {Component} from '@angular/core';
import {
    AlertController, IonicPage, LoadingController, NavController, NavParams, Platform, ToastController,
    ViewController
} from 'ionic-angular';
import {SystemService} from "../service/SystemService";
import {BasePage} from "../BasePage";
import {UserManager} from "../../framework/manager/UserManager";
import {Log} from "../../framework/utils/Log";

/**
 * Generated class for the IndexQueryPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-index-query',
    templateUrl: 'index-query.html',
    providers: [SystemService]
})
export class IndexQueryPage extends BasePage {
    private static TAG: string = 'IndexQueryPage';
    planList: any = [];
    userId: string;
    year: string;
    unidId: string;
    searchName:string;
    private branchId: any;
    constructor(public navCtrl: NavController, public navParams: NavParams, public view: ViewController, public sys: SystemService, public platform: Platform,
                public loadingCtrl: LoadingController,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);
        this.unidId = UserManager.getInstance().user.unitId;
        this.userId = UserManager.getInstance().user.userId;
        this.year = UserManager.getInstance().user.year;
        this.branchId = UserManager.getInstance().user.branchId;
    }
    ionViewWillEnter(){
        Log.w(IndexQueryPage.TAG, 'ionViewWillEnter IndexQueryPage');
        this.year = UserManager.getInstance().user.year;
        let data: Object = {
            BuMenBH: this.branchId,
            DanWeiBH: this.unidId,
            NianDu: this.year,
            YongHuBH: this.userId,
            ZhiBiaoMC: ''
        };
        this.getIndexSelect(data);
    }
    ionViewDidLoad() {
        // Log.w(IndexQueryPage.TAG, 'ionViewDidLoad IndexQueryPage');
        // let data: Object = {
        //     BuMenBH: this.branchId,
        //     DanWeiBH: this.unidId,
        //     NianDu: this.year,
        //     YongHuBH: this.userId,
        //     ZhiBiaoMC: ''
        // };
        // this.getIndexSelect(data);
    }

    getIndexSelect(data) {
        this.showLoading(true, '数据加载中');

        this.sys.getIndexSelect(data).subscribe((res: any) => {
            Log.printObj(IndexQueryPage.TAG, 'result==', res);
            if (res.msgCode == "1") {
                this.planList = res.list;
                // if(this.planList.length==0){
                //     this.showError('你搜索的指标不存在');
                // }
            } else {
                this.showError('数据加载失败');
            }
            this.showLoading(false, '数据加载中');

        }, error => {
            this.showLoading(false, '数据加载中');
            this.showError('数据加载失败');
        })
    }
    search(str){
        let data: any = {
            BuMenBH: this.branchId,
            DanWeiBH: this.unidId,
            NianDu: this.year,
            YongHuBH: this.userId,
            ZhiBiaoMC: str
        };
        this.getIndexSelect(data);
    }

}
